<?php
session_start();
include("../includes/conn.php");
$dbconn->admin_logincheck();
$adminid=$_SESSION['adminid'];
$id = isset($_GET['id'])?$_GET['id']:'';
$act = isset($_GET['act'])?$_GET['act']:'';
if($_GET['act']=='edit')
{
	$selrow=$dbconn->fetch($dbconn->query("select * from ".DBQIAN."gift where id=$id"));
}
//添加
if($_GET['act']=='add'){
	//print_r($_FILES['img']);exit;
	if ($_FILES&&!empty($_FILES['img']['name']))
	{
		$result = upload($_FILES['img']);
		//print_r($result);exit;
		if ($result&&$result['result'] == 1)
		{
			$img = $result['path'];
		}
		else 
		{
			$dbconn->showalert("图片上传失败","gift.php",1);	exit;
		}
	}
	else 
	{
		$img = $_POST['img1'];
	}
	//echo $img;exit;
	$name=$_POST['name'];
	$getway = $_POST['getway'];
	$credit = $_POST['credit'];
	$type = $_POST['type'];	
	$desc = htmlspecialchars($_POST['content1']);//exit;
	$time = date('Y-m-d H:i:s',time());
//	echo 'insert into '.DBQIAN.'gift(title,desc,time,content,url) values(\''.$title.'\',\''.$desc.'\',\''.$time.'\',\''.$content.'\',\''.$url.'\')';exit;
	$dbconn->noretquery("update ".DBQIAN."gift set `name`='$name',`desc`='$desc',`img`='$img',`time`='$time',getway=".$getway.",credit=".$credit.",type=".$type." where id=".$id);
	$dbconn->showalert("保存成功","gift.php",1);
}

function upload($file)
{
	$picname = $file['name'];
	$picsize = $file['size'];
	if ($picname != "") 
	{
		if ($picsize > 1024000) 
		{
			echo '图片大小不能超过1M';
			exit;
		}
		$type = strstr($picname, '.');
		if ($type != ".gif" && $type != ".jpg" && $type != ".png") 
		{
			echo '图片格式不对！';
			exit;
		}
		$rand = rand(100, 999);
		$pics = date("YmdHis") . $rand . $type;
		//上传路径
		$pic_path = "../user/images/gift/". $pics;
		$result = move_uploaded_file($file['tmp_name'], $pic_path);
	}
	$arr = array(
		'name'=>$picname,
		'path'=>$pic_path,
		'pic'=>$pics,
		'size'=>$size,
		'result'=>$result
	);
	return $arr;
}

;echo '<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>欢迎登录后台管理系统</title>
<link href="css/base.css" rel="stylesheet" type="text/css" />
<link href="css/right.css" rel="stylesheet" type="text/css">
<!--<link href="css/select.css" rel="stylesheet" type="text/css">-->
<link rel="stylesheet" href="k/themes/default/default.css" />
<link rel="stylesheet" href="k/plugins/code/prettify.css" />
<script charset="utf-8" src="k/kindeditor.js"></script>
<script charset="utf-8" src="k/lang/zh_CN.js"></script>
<script charset="utf-8" src="k/plugins/code/prettify.js"></script>
<script src="js/jquery.js"></script>
<script src="js/select-ui.min.js"></script>
<script type="text/javascript" src="js/uploadPreview.js"></script>
<script>
$(document).ready(function(){
   $(function(){
   		$(\'#time\').datetimepicker();
   		/*$(\'#img\').change(function(){
			alert(123);return false;
			var objUrl = getObjectURL(this.files[0]) ;
			console.log("objUrl = "+objUrl) ;
			if (objUrl) {
				$("#img0").attr("src", objUrl) ;
			}
		}) ;*/
   });
   
});

function getObjectURL(file) {
	var url = null ; 
	if (window.createObjectURL!=undefined) { // basic
		url = window.createObjectURL(file) ;
	} else if (window.URL!=undefined) { // mozilla(firefox)
		url = window.URL.createObjectURL(file) ;
	} else if (window.webkitURL!=undefined) { // webkit or chrome
		url = window.webkitURL.createObjectURL(file) ;
	}
	return url ;
}
</script>
</head>
<body>
<div class="place"> <span>位置：</span>
  <ul class="placeul">
    <li><a href="index.php">首页</a></li>
    <li><a href="gift.php">礼品管理</a></li>
    <li>添加</li>
  </ul>
</div>
<div class="formbody">
  <div class="formtitle"><span>礼品管理添加</span></div>
  <form name="radd" method="post" action="?act=add&id=';echo $selrow['id'];;echo '" enctype="multipart/form-data">
  <ul class="forminfo">
    <li>
      <label>礼品名称</label>
      <input name="name" type="text" value="';echo $selrow['name'];;echo '" class="dfinput" />
      <i></i>
    </li>
     <li>
      <label>获取方式</label>
	      <label><input name="getway" type="radio" value="1"';if($selrow['getway']==1){  echo 'checked="checked"';} echo ' />邮寄方式</label>
	     <!-- <label><input name="getway" type="radio" value="2" />现场领取</label>
	      <label><input name="getway" type="radio" value="3" />1111</label>
	      <label><input name="getway" type="radio" value="3" />3333</label>-->
      <i></i>
    </li>
     <li>
      <label>所需积分</label>
      <input name="credit" type="text" value="';echo $selrow['credit'];;echo '" placeholder="请直接输入分值；例：200" class="dfinput" />
      <i></i>
    </li>
     <li>
      <label>奖品类型</label>
      <div style="border:1px;">
	      <select name="type" style="width:347px; height:30px; font-size:14px;border:1px solid green; position:absolute; overflow:hidden">
	      	<option value="1" ';if($selrow['getway']==1){  echo 'selected="selected"';} echo ' >普通奖品</option>
	      	<option value="2" ';if($selrow['getway']==2){  echo 'selected="selected"';} echo ' >抽奖</option>
	      	<option value="3" ';if($selrow['getway']==3){  echo 'selected="selected"';} echo ' >红包</option>
	      </select>
      </div>
      <i></i>
    </li>
    <li>
      <label>图片</label>
	   <input type="file" class="dfinput" style="background:#ccc;text-align:center;border:1px dotted #ccc;" id="img" name="img"  />
	   <br/><img style="margin-left:90px;" src="';echo $selrow['img'];; echo '"';;if ($selrow['img']){echo "width=120px height=120px";} echo 'id="img0" >
	</li>
    <li style="width:98%;height:242px;">
    	<label>礼品描述</label>
    	<textarea name="content1" style="width:680px;height:240px;visibility:hidden;">';echo $selrow['desc'];;echo '</textarea>
		<br />
    	<i></i>
    </li>
    <li>
      <label>&nbsp;</label>
      <input type="hidden" value="';echo $selrow['img'];; echo '"  name="img1"  />
      <input name="button" type="submit" class="btn" value="确认保存"/>
    </li>
  </ul>
  </form>
</div>
<script>
	KindEditor.ready(function(K) {
		var editor1 = K.create(\'textarea[name="content1"]\', {
			cssPath : \'k/plugins/code/prettify.css\',
			uploadJson : \'k/php/upload_json.php\',
			fileManagerJson : \'k/php/file_manager_json.php\',
			allowFileManager : true,
			afterCreate : function() {
				var self = this;
				K.ctrl(document, 13, function() {
					self.sync();
					K(\'form[name=radd]\')[0].submit();
				});
				K.ctrl(self.edit.doc, 13, function() {
					self.sync();
					K(\'form[name=radd]\')[0].submit();
				});
			}
		});
		prettyPrint();
	});
</script>
</body>
</html>'
?>